using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.Core.DAL;

namespace Portal.GUI.EditoralOffice.MainOffce.NewsEditModules.RelatedNews
{
    public partial class RelatedNews : Portal.API.Module
    {
        protected string title = "";
        protected string titleColor = "";
        protected string titleBackgroundColor = "";
        protected string titleHeight = "";
        protected string newsHeight = "";
        protected string width = "";
        protected string backgroundColor = "";
        protected string borderColor = "";
        protected string borderWidth = "";
        protected string paddingLeft = "";
        protected string paddingRight = "";
        protected string paddingTop = "";
        protected string paddingBottom = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            bindData();
        }

        private void bindData()
        {
            LoadRuntimeSettings();

            title = Convert.ToString(ReadRuntimeConfig(true, "Title"));
            titleColor = Convert.ToString(ReadRuntimeConfig(true, "TitleColor"));
            titleBackgroundColor = Convert.ToString(ReadRuntimeConfig(true, "TitleBackgroundColor"));
            titleHeight = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "TitleHeight")));
            newsHeight = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "NewsHeight")));
            width = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "Width")));
            if (!String.IsNullOrEmpty(width))
            {
                width = "width='" + width + "'";
            }
            backgroundColor = Convert.ToString(ReadRuntimeConfig(true, "BackgroundColor"));
            borderColor = Convert.ToString(ReadRuntimeConfig(true, "BorderColor"));
            borderWidth = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "BorderWidth")));
            paddingLeft = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "PaddingLeft")));
            paddingRight = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "PaddingRight")));
            paddingTop = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "PaddingTop")));
            paddingBottom = formatDimension(Convert.ToString(ReadRuntimeConfig(true, "PaddingBottom")));

            string newsIdStr = Convert.ToString(ReadRuntimeConfig(true, "NewsID")).Trim();
            bindNews(newsIdStr);
        }

        private void bindNews(string newsIdStr)
        {
            if (newsIdStr != "")
            {
                DataTable newsTable = new DataTable();
                newsTable.Columns.Add("News_ID", typeof(Int64));
                newsTable.Columns.Add("News_Title", typeof(string));
                Hashtable table = getNews(newsIdStr);
                string[] newsIds = newsIdStr.Split(',');
                foreach (string newsId in newsIds)
                {
                    DataRow row = newsTable.NewRow();
                    row[0] = newsId;
                    row[1] = table[newsId];
                    newsTable.Rows.Add(row);
                    //rptNews.Items.Add(new ListItem(Convert.ToString(table[newsId]), newsId));
                }
                rptNews.DataSource = newsTable;
                rptNews.DataBind();
            }
        }

        private string formatDimension(string d)
        {
            try
            {
                Convert.ToInt32(d);
                d += "px";
            }
            catch { }
            return d;
        }
        private Hashtable getNews(string newsIds)
        {
            Hashtable table = new Hashtable();
            using (MainDB db = new MainDB())
            {
                string sql = "Select News_ID, News_Title from News where News_ID in (" + newsIds + ")";
                DataTable dt = db.SelectQuery(sql);
                foreach (DataRow row in dt.Rows)
                {
                    table.Add(Convert.ToString(row["News_ID"]), Convert.ToString(row["News_Title"]));
                }
                db.Close();
            }
            return table;
        }
    }
}